package cn.biplam.common.client;

import java.io.Serializable;

import cn.biplam.common.client.paging.PagingFactory;
import cn.biplam.common.paramters.PersisParam;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.sqlresult.SqlResult;

/**
 * 分页信息类
 *  <ul>
 *  初始化类   Paging mPaging = new Paging();
 *  设置      mPaging.set();
 *  取总页数   mPaging.getTotle();
 *  取总行数   mPaging.getTotalLine();
 *  是否有
 *  </ul>
 * @author WangSong 2009-6-29上午10:40:31
 * @version 1.0
 */
public class Paging implements Serializable {
	
	/** */
	private static final long serialVersionUID = 5668949537289395005L;
	/** 总页数 */
	private int totalPage;
	/** 总行数 */
	private int totalLine;
	/** 默认每页显示行数 */
	private int defaultPageLine=20;
	/** 是否有上一页 */
	private boolean isHavePrePage = false;
	/** 是否有下一页 */
	private boolean isHaveNextPage = false;
	/** 操作id*/
	private String optId;
	private int pageNum=1;
	/** 页数的参数名称 */
	public static final String pageNumParam="pageNum";
	/** 每页显示记录数的参数名称 */
	public static final String showPageLine="showPageLine";
	
	
	public Paging(String defaultPageLine){
		if(defaultPageLine!=null && !"".equals(defaultPageLine.trim())){
			this.defaultPageLine = TOOLS.stringToInt(defaultPageLine);
		}
	}
	
	/**
	 * 取总页数
	 * @return
	 */
	public int getTotal(){
		return this.totalPage;
	}
	
	/**
	 * 取总行数
	 * @return
	 */
	public int getTotalLine(){
		return this.totalLine;
	}
	
	/**
	 * 是否有下一页
	 * @return boolean
	 */
	public boolean isHaveNextPage(){
		return this.isHaveNextPage;
	}
	
	/**
	 * 是否有上一页
	 * @return boolean
	 */
	public boolean isHavePrePage(){
		return this.isHavePrePage;
	}
	
	/**
	 * 设置分页信息
	 * @param selectResult
	 * @param pageNum
	 * @throws ErrorMessage
	 */
	public void set(SqlResult selectResult,String userId,String optId) throws ErrorMessage{
		this.pageNum = getFromPersist(userId, optId);
		this.optId = optId;
		if(selectResult!=null && selectResult.size()>0){
			/**总行数*/
			this.totalLine = TOOLS.stringToInt(selectResult.get(0,"totle"));
			/**总页数*/
			this.totalPage = (this.totalLine+this.defaultPageLine-1)/this.defaultPageLine;
			/**是否有上一页 */
			if(this.pageNum>1){
				this.isHavePrePage = true;
			}
			/**是否有下一页 */
			if(this.pageNum<this.totalPage){
				this.isHaveNextPage = true;
			}
		}
	}
	
	/**
	 * sql分页封装，取指定页数的数据
	 * @param sql
	 * @return
	 */
	public String getSqlPart(String sql,int pageNum){
		StringBuilder sb = new StringBuilder();
		sb.append("select * from (");
		sb.append( sql +") p_g");
		sb.append(" where p_g.orid>="+this.getStartOrid(pageNum));
		sb.append("   and p_g.orid<="+this.getEndOrid(pageNum));
		return sb.toString();
	}
	
	/**
	 * 根据数据库Rowid方式取分页信息
	 * @param sql   查询sql
	 * @param pageNum  页码
	 * @param dbType  数据库类型
	 * @param userId  用户id
	 * @param optId 操作id
	 * @return 分页包装后的sql
	 * @throws ErrorMessage
	 */
	public SqlResult getSqlPartByDb(String sql,Database database,DbResource mDbResource,String userId,String optId,Paging mPaging) throws ErrorMessage{
		pageNum = getFromPersist(userId, optId);
		if(pageNum>mPaging.getTotal()){
			pageNum = mPaging.getTotal();
			mPaging.setCurrentPageNum(pageNum);
		}
		return new PagingFactory(mDbResource).getPaging(database, sql, getStartOrid(pageNum), getEndOrid(pageNum));
	}
	
	/**
	 * 取总页数的sql,替换原sql中的字段信息为count(*)
	 * @param sql
	 * @return
	 */
	public String getSqlTotle(String sql){
		StringBuilder sb = new StringBuilder();
		String temp = sql;
		if(sql!=null){
			temp = temp.toUpperCase();
		}else{
			return sql;
		}
		int ordernum = temp.indexOf("ORDER");
		if(ordernum>0){
			sql = sql.substring(0, ordernum);
		}
		
		sb.append("select count(*)  as totle from(");
    	sb.append(  sql);
    	sb.append(" ) totle_");
    	return sb.toString();
	}
	
	/**
	 * 取开始的排序号
	 * @param pageNum
	 * @return
	 */
	private int getStartOrid(int pageNum){
		return pageNum*this.defaultPageLine-this.defaultPageLine+1;
	}
	
	/**
	 * 取结束的排序号
	 * @param pageNum
	 * @return
	 */
	private int getEndOrid(int pageNum){
		return pageNum*this.defaultPageLine;
	}
	
	/**
	 * @return 是否第一页
	 */
	public boolean isFirstPage(){
		return this.getCurrentPageNum()>1?false:true;
	}
	
	/**
	 * @return 是否最后一页
	 */
	public boolean isLastPage(){
		return this.getCurrentPageNum()<this.getTotal()?false:true;
	}
	
	/**
	 * 取当前页数
	 * @return
	 */
	public int getCurrentPageNum(){
		return this.pageNum;
	}
	
	/**
	 * 设置当前页数
	 * @return
	 */
	public void setCurrentPageNum(int pageNum){
		this.pageNum = pageNum;
	}
	
	/**
	 * 从持久层取页数信息
	 * @param userId 用户id
	 * @param optId  操作id
	 * @param pageNum 页码
	 * @throws ErrorMessage
	 */
	private int getFromPersist(String userId,String optId) throws ErrorMessage{
		if(PersisParam.get(userId,"1", optId+"page")==null){
			PersisParam.set(userId,"1", optId+"page", 1);
		}
		return (Integer)PersisParam.get(userId,"1", optId+"page");
	}
	
	/**
	 * 设置页数信息到持久层
	 * @param userId 用户id
	 * @param optId  操作id
	 * @param pageNum 页码
	 * @throws ErrorMessage
	 */
	public static void setPersist(String userId,String optId,int pageNum) throws ErrorMessage{
		if(pageNum<1){
			pageNum = 1;
		}
		PersisParam.set(userId,"1", optId+"page", pageNum);
	}

	public String getOptId() {
		return optId;
	}
}
